package com.lframework.xingyun.basedata.mappers;

import com.lframework.starter.web.core.mapper.BaseMapper;
import com.lframework.xingyun.basedata.dto.make.ProductMakeDto;
import com.lframework.xingyun.basedata.entity.*;

import java.util.List;

/**
 * <p>
 * 商品与制作方法关系表 Mapper 接口
 * </p>
 *
 */
public interface ProductMakeMapper extends BaseMapper<ProductMake> {

    default List<ProductMakeDto> selectByMakeId(String makeId) {
        return defaultJoinWrapper()
                .selectAll(ProductMake.class)
                .selectAs(Product::getId, ProductMakeDto::getProductId)
                .selectAs(Product::getName, ProductMakeDto::getName)
                .selectAs(Product::getUnit,  ProductMakeDto::getUnit)
                .selectAs(Unit::getUnitName, ProductMakeDto::getUnitName)
                .selectAs(Product::getSpec, ProductMakeDto::getSpec)
                .selectAs(ProductCategory::getName, ProductMakeDto::getCategoryName)
                .selectAs(ProductBrand::getName, ProductMakeDto::getBrandName)
                .selectAs(ProductRetail::getPrice, ProductMakeDto::getRetailPrice)
                .selectAs(Product::getAvailable, ProductMakeDto::getAvailable)
                .leftJoin(Product.class, on -> on
                        .eq(Product::getCode, ProductMake::getCode)
                        .eq(Product::getSkuCode, ProductMake::getSkuCode)
                        .isNull(Product::getShopId))
                .leftJoin(Unit.class, Unit::getId, Product::getUnit)
                .leftJoin(ProductCategory.class, ProductCategory::getId, Product::getCategoryId)
                .leftJoin(ProductBrand.class, ProductBrand::getId, Product::getBrandId)
                .leftJoin(ProductRetail.class, ProductRetail::getId, Product::getId)
                .eq(ProductMake::getMakeId, makeId)
                .list(ProductMakeDto.class);
    }

    default Boolean selectNormalFlag(String productId) {
        return defaultJoinWrapper()
                .leftJoin(Product.class, on -> on
                        .eq(Product::getCode, ProductMake::getCode)
                        .eq(Product::getSkuCode, ProductMake::getSkuCode)
                        .isNull(Product::getShopId))
                .eq(Product::getId, productId)
                .count() == 0;
    }

}

